package org.other.testPk;

/**
 * @Author yu
 * @Date 2022/7/8
 */
public class Demo2 {

    public static void main(String[] args) {

        int[] ints = {-7, -6, -3, -2, 3, 5, 7, 9};

        System.out.println(method(ints));

    }

    public static Integer method(int[] arr) {

        if (arr[0] >= 0) {
            return arr[0];
        }
        if (arr[arr.length - 1] <= 0) {
            return arr[arr.length];
        }

        int left = 0, right = arr.length - 1, mid;
        while (left <= right) {

            mid = (left + right) / 2;
            if (arr[mid] <= 0 && arr[mid + 1] > 0) {
                return Math.abs(arr[mid]) >= arr[mid + 1] ? arr[mid + 1] : Math.abs(arr[mid]);
            }
            if (arr[mid] > 0) {
                right = mid - 1;
            }
            if (arr[mid] < 0) {
                left = mid + 1;
            }

        }
        return null;
    }

}
